﻿package cn.smbms.service.user;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.user.UserDao;

import cn.smbms.pojo.User;
import cn.smbms.tools.ACT;


@Service("userServiceImpl")
/*@Transactional*/
public class UserServiceImpl implements UserService{
	private static Logger logger=Logger.getLogger(UserServiceImpl.class);
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	/*@Transactional(propagation=Propagation.REQUIRED,rollbackFor={RuntimeException.class})*/
	@Override
	public boolean add(User user) {
		// TODO Auto-generated method stub
		
		boolean flag = false;
		
		try {
			
			int updateRows = userDao.add(user);
			
			if(updateRows > 0){
				flag = true;
				System.out.println("add success!");
			}else{
				System.out.println("add failed!");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException();
			
		}finally{
			//在service层进行connection连接的关闭
			ACT.close();
		}
		return flag;
	}
	@Override
	public User login(String userCode, String userPassword) {
		// TODO Auto-generated method stub
		
		User user = null;
		try {
			
			user = userDao.getLoginUser(userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ACT.close();
		}
		
		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}
		
		return user;
	}
	@Override
	public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		
		List<User> userList = null;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		System.out.println("currentPageNo ---- > " + currentPageNo);
		System.out.println("pageSize ---- > " + pageSize);
		try {
			currentPageNo = (currentPageNo-1)*pageSize;
			userList = userDao.getUserList(queryUserName,queryUserRole,currentPageNo,pageSize);
			logger.debug(userList.get(0).getAddress());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ACT.close();
		}
		return userList;
	}
	@Override
	public User selectUserCodeExist(String userCode) {
		// TODO Auto-generated method stub
		
		User user = null;
		try {
			
			user = userDao.getLoginUser(userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ACT.close();
		}
		return user;
	}
	@Override
	public boolean deleteUserById(Integer delId) {
		// TODO Auto-generated method stub
	
		boolean flag = false;
		try {
			
			if(userDao.deleteUserById(delId) > 0){
				flag = true;
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ACT.close();
		}
		return flag;
	}
	@Override
	public User getUserById(String id) {
		// TODO Auto-generated method stub
		User user = null;
		
		try{
			
			user = userDao.getUserById(id);
			Date de=user.getBirthday();
			Date da=new java.sql.Date(de.getTime());
			user.setBirthday(da);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			user = null;
		}finally{
			ACT.close();
		}
		return user;
	}
	@Override
	public boolean modify(User user) {
		// TODO Auto-generated method stub
		
		boolean flag = false;
		try {
			
			if(userDao.modify(user) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ACT.close();
		}
		return flag;
	}
	
	
	
	@Override
	public boolean updatePwd(int id, String pwd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		
		try{
			
			if(userDao.updatePwd(id,pwd) > 0){
				flag = true;
			}
				
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}finally{
			/*ACT.close();*/
		}
		return flag;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		// TODO Auto-generated method stub
	
		int count = 0;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		try {
			
			count = userDao.getUserCount(queryUserName,queryUserRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ACT.close();
		}
		return count;
	}
	
}
